//
//  AXYxtyViewController.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/12.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXYxtyViewController.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "Header.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "AXYxtyDetailsTableViewCell.h"
#import "AXYxtyModel.h"
#import "AXYxtyIntroduceTableViewCell.h"
#import "AXYxtyIntroduceModel.h"


@interface AXYxtyViewController ()<UITableViewDelegate,UITableViewDataSource>
/** 放班级作业模型 */
@property (nonatomic, strong) NSMutableArray<AXYxtyModel *>  *modelsArray;
@property (nonatomic ,strong) UITableView *tableView;
/** 测试数据 */
@property (nonatomic, strong) NSArray<NSString *> *images;
/** 表头 */
@property (nonatomic ,strong) UIView *headView;
/** headImageView */
@property (nonatomic ,strong) UIImageView *imageViewInHeadView;
/** 促销按钮 */
@property (nonatomic ,strong) UIButton *salesButton;


@end

@implementation AXYxtyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self setupNavigationItems];
  
    //测试数据
    self.images = @[@"m1.jpeg",@"m2.jpeg",@"m3.jpeg",@"m4.jpeg",@"m5.jpeg",@"m6.jpeg",@"m7.jpeg",@"m8.jpeg"];
    [self creatView];
    [self setlayout];
    
    self.tableView.tableHeaderView = _headView;
    [_headView setNeedsLayout];
    [_headView layoutIfNeeded];//刷新布局
    //CGFloat height = [_headView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    _headView.frame = CGRectMake(0, NAVIGATION_HEIGHT, [UIScreen mainScreen].bounds.size.width, 200);//headView 的大小
    self.tableView.tableHeaderView = _headView;
  
}

- (void)creatView {
    /** 表头创建 */
      _headView = [[UIView alloc]init];
    /** 表头图标 */
    self.imageViewInHeadView = [[UIImageView alloc]init];
    self.imageViewInHeadView.contentMode =UIViewContentModeScaleAspectFill;
    self.imageViewInHeadView.clipsToBounds=YES;//
    self.imageViewInHeadView.image = [UIImage imageNamed:@"y1.jpeg"];
    [self.headView addSubview:self.imageViewInHeadView];
    /** 促销按钮 */
    self.salesButton = [[UIButton alloc]init];
    self.salesButton.backgroundColor = [UIColor orangeColor];
    [self.salesButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.salesButton setTitle:@"距结束：12:10:10" forState:UIControlStateNormal];
    [self.imageViewInHeadView  addSubview:self.salesButton];
    
    
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[AXYxtyDetailsTableViewCell class] forCellReuseIdentifier:[AXYxtyDetailsTableViewCell reuseID]];
    [self.tableView registerClass:[AXYxtyIntroduceTableViewCell class] forCellReuseIdentifier:[AXYxtyIntroduceTableViewCell reuseID]];
    [self.view addSubview:self.tableView];
  
    
}

- (void)setlayout {
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, Hs)
    .widthIs(Wi)
    .heightIs(Hi-Hs-Htb);
    
    self.imageViewInHeadView.sd_layout
    .leftSpaceToView(self.headView, 0)
    .topSpaceToView(self.headView, 0)
    .widthIs(SCREEN_WIDTH)
    .heightIs(200);
    
    self.salesButton.sd_layout
    .rightSpaceToView(self.imageViewInHeadView, 0)
    .bottomSpaceToView(self.imageViewInHeadView, 0)
    .widthIs(SCREEN_WIDTH/2)
    .heightIs(40);
    
}

- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
  
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    titleLabel.text = @"优先体验";
    self.navigationItem.titleView = titleLabel;
    
    
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - tableViewDelegate & tableViewDatasorce



//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        
        return 1;
    }
    
    return 8;
}

/** cell的高度 */

/** cell 自适应第二步 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXYxtyIntroduceTableViewCell class] contentViewWidth:SCREEN_WIDTH];
        //return 200;
    }
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXYxtyDetailsTableViewCell class] contentViewWidth:SCREEN_WIDTH];
  
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        AXYxtyIntroduceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXYxtyIntroduceTableViewCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
    
    AXYxtyDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXYxtyDetailsTableViewCell reuseID] forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.img.image = [UIImage imageNamed:self.images[indexPath.row]];
    return cell;
    
    
}


//-(NSMutableArray<AXYxtyModel *> *)modelsArray{
//    
//    if (!_modelsArray) {
//        _modelsArray = [NSMutableArray<AXYxtyModel *> array];
//    }
//    return _modelsArray;
//    
//}




@end
